package com.mioto.ld.module.center.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.center.model.*;
import com.mioto.ld.module.device.dao.DeviceDao;
import com.mioto.ld.module.device.model.DeviceAddress;
import com.mioto.ld.module.device.model.DeviceStatusStatisticVO;
import com.mioto.ld.module.device.service.IDeviceAddressService;
import com.mioto.ld.module.device.service.IDeviceFaultLogService;
import com.mioto.ld.module.device.service.IDeviceService;
import com.mioto.ld.module.product.service.IProductService;
import com.mioto.ld.module.statistics.StatisticsPO;
import com.mioto.ld.module.statistics.model.DeviceFaultTypeStatisticsVO;
import com.mioto.ld.module.statistics.model.SameCompareStatisticsVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author mioto-qinxj
 * @date 2024/7/9
 * @desc
 */
@Service
public class DataCenterService {

    private final int index = 12;
    private final String format = "yyyy-MM";
    @Resource
    private IProductService productService;
    @Resource
    private IDeviceService deviceService;
    @Resource
    private DeviceDao deviceDao;
    @Resource
    private IDeviceAddressService deviceAddressService;
    @Resource
    private IDeviceFaultLogService deviceFaultLogService;

    public DataCenterVO dataCenter(){
        DataCenterVO dataCenterVO = new DataCenterVO();
        dataCenterVO.setSaleAreas(saleAreaStatistics());
        dataCenterVO.setSaleRanks(saleRankStatistics());
        dataCenterVO.setSaleTrends(saleTrendStatistics());
        dataCenterVO.setDeviceStatus(deviceStatusStatistics());
        dataCenterVO.setDeviceFaultTypeStatistics(faultTypeStatistics());
        dataCenterVO.setDeviceAddresses(deviceAddress());
        dataCenterVO.setDeviceFaultVO(findDeviceFault());
        return dataCenterVO;
    }

    /**
     * 设备状态统计
     */
    private DeviceStatusStatisticVO deviceStatusStatistics(){
        return deviceService.statusStatistics(null);
    }


    /**
     * 产品销量排行
     */
    private List<ProductSaleRankVO> saleRankStatistics(){
        return productService.productSaleStatistics();
    }

    /**
     * 产品销量趋势
     */
    private List<List<ProductSaleTrendVO>> saleTrendStatistics(){
        Date endDate = DateUtil.dateSecond();
        Date temp = DateUtil.offsetMonth(endDate,-index + 1);
        Date startDate =DateUtil.parse(DateUtil.format(temp,format),format);
        List<ProductSaleTrendVO> productSaleTrendVOS = productService.saleTrendStatistics(startDate,endDate);
        List<List<ProductSaleTrendVO>> list = CollUtil.groupByField(productSaleTrendVOS,"productName");
        List<List<ProductSaleTrendVO>> newList = new ArrayList<>(list.size());
        ProductSaleTrendVO[] array;
        for (List<ProductSaleTrendVO> saleTrendVOS : list) {
            array = buildTempArray(startDate,saleTrendVOS.get(0).getProductName());
            CollUtil.sortByProperty(saleTrendVOS,"formatDate");
            newList.add(reBuild(saleTrendVOS,array,index));
        }
        return newList;
    }

    /**
     * 设备销量地区分布
     */
    private List<DeviceSaleAreaVO> saleAreaStatistics(){
        return productService.saleAreaStatistics();
    }

    /**
     * 设备故障分布
     */
    private List<DeviceFaultTypeStatisticsVO> faultTypeStatistics(){
        return deviceDao.faultTypeStatistics(null);
    }

    /**
     * 设备地址列表
     */
    private List<DeviceAddress> deviceAddress(){
        return deviceAddressService.findList(null);
    }

    /**
     * 设备故障率
     */
    private DeviceFaultVO findDeviceFault(){
        DeviceFaultVO deviceFaultVO = new DeviceFaultVO();
        Date endDate = new Date();
        int[] deviceFaults = deviceFaultLogService.findDeviceFault(DateUtil.beginOfYear(endDate),DateUtil.beginOfMonth(endDate),endDate);
        if (ArrayUtil.isNotEmpty(deviceFaults) && deviceFaults.length == 3){
            deviceFaultVO.setYearFault((double) deviceFaults[1]/deviceFaults[0]);
            deviceFaultVO.setMouthFault((double)deviceFaults[2]/deviceFaults[0]);
        }
        return deviceFaultVO;
    }

    private List<ProductSaleTrendVO> reBuild(List<ProductSaleTrendVO> productSaleTrendVOS, ProductSaleTrendVO[] array, int index){
        List<ProductSaleTrendVO> newList = new ArrayList<>(index);
        int month;
        int i = 0;
        for (ProductSaleTrendVO productSaleTrendVO : productSaleTrendVOS) {
            month = productSaleTrendVO.getMonth();
            while (true) {
                if (newList.size() == index){
                    break;
                }
                if (month == array[i].getMonth()) {
                    newList.add(productSaleTrendVO);
                    i++;
                    break;
                } else {
                    newList.add(array[i++]);
                }
            }
        }

        if (newList.size() < index){
            for (;i < index;){
                newList.add(array[i++]);
            }
        }
        return newList;
    }

    private ProductSaleTrendVO[] buildTempArray(Date startDate,String productName){
        ProductSaleTrendVO[] array = new ProductSaleTrendVO[index];
        DateTime dateTime;
        for (int i = 0; i < index; i++) {
            dateTime =  DateUtil.offsetMonth(startDate,i);
            array[i] = new ProductSaleTrendVO();
            array[i].setMonth(buildTimeStr(startDate,i));
            array[i].setProductName(productName);
            array[i].setFormatDate(DateUtil.format(dateTime, format));
        }
        return array;
    }

    private int buildTimeStr(Date startDate, int index) {
        DateTime dateTime = DateUtil.offsetMonth(startDate,index);
        return dateTime.month() + 1;
    }
}
